package com.client.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.client.model.IPayData;
import com.client.model.IPayOrder;
import com.manage.config.Config;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ModelGenerator;

public class PayDataServiceImpl extends AbstractService implements
		PayDataService {
	private Logger logger;
	
	public PayDataServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}
	public List<IPayOrder> getPayOrder(String date1,String date2,String mobile,String orderId)
	{
//		cpid int,
//		appid varchar(128),
//		goodscode varchar(128),
//		goodsname varchar(128),
//		chid varchar(128),
//		requestfee varchar(32),
//		successfee varchar(32),
		String sql = "select id,orderid,suborderid,userid,mobileNo,carrier," +
				"ordertype,codetype,spid,spcodeid,province,region,feevalue," +
				"channelid,projectid,report,ordertime orderTime1,reporttime reporttime1" +
				",'0' errorNum,ordertime ordertime,reporttime reporttime," +
				"cpid,appid,goodscode,goodsname,chid,requestfee,successfee" +
				" from ipayorder where ordertime between '"+date1+"' and '"+date2+"' ";
		if(mobile!=null && mobile.length()>0)
			sql+=" and mobileNo='"+mobile+"'";
		if(orderId!=null && orderId.length()>0)
			sql+=" and orderid='"+orderId+"'";
		sql+=" order by ordertime desc";
		
		System.out.println(sql);
		ModelGenerator<IPayOrder> dbcpmg = new ModelGenerator<IPayOrder>(IPayOrder.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	@Override
	public List<IPayData> getPayDataByContent(String date1,String date2,
			int channel, int project, int spId, int spCodeId, String province,
			String region,String dimension,Account operator,int cpid,String appnumber) {
		String sql = "";
		{
			String gb = "";
			List<String> fl = new ArrayList<String>();
			
			if (!checkQueryDimension(dimension, "day"))
				gb += "0 ";
			else
				fl.add("day");
			gb += "day,";
			
			if (!checkQueryDimension(dimension, "hour"))
				gb += "0 ";
			else
				fl.add("hour");
			gb += "hour,";
			
			if (!checkQueryDimension(dimension, "minute"))
				gb += "0 ";
			else
				fl.add("minute");
			gb += "minute,";
			
			
			
			if (!checkQueryDimension(dimension, "carrier"))
				gb += "0 ";
			else
				fl.add("carrier");
			gb += "carrier,";
			if (!checkQueryDimension(dimension, "codetype"))
				gb += "0 ";
			else
				fl.add("codetype");
			gb += "codetype,";
			if (!checkQueryDimension(dimension, "spid"))
				gb += "0 ";
			else
				fl.add("spid");
			gb += "spid,";
			if (!checkQueryDimension(dimension, "spcodeid"))
				gb += "0 ";
			else
				fl.add("spcodeid");
			gb += "spcodeid,";
			
			
			
			if (!checkQueryDimension(dimension, "channel"))
				gb += "-1 ";
			else
				fl.add("channel");
			gb += "channel,";

			if (!checkQueryDimension(dimension, "project"))
				gb += "-1 ";
			else
				fl.add("project");
			gb += "project,";

			if (!checkQueryDimension(dimension, "province"))
				gb += "'' ";
			else
				fl.add("province");
			gb += "province,";

			if (!checkQueryDimension(dimension, "region"))
				gb += "'' ";
			else
				fl.add("region");
			gb += "region,";
			
			
			
			if (!checkQueryDimension(dimension, "cpid"))
				gb += "'' ";
			else
				fl.add("cpid");
			gb += "cpid,";
			
			if (!checkQueryDimension(dimension, "appid"))
				gb += "'' ";
			else
				fl.add("appid");
			gb += "appid,";
			
			if (!checkQueryDimension(dimension, "goodscode"))
				gb += "'' goodscode,'' goodsname,";
			else
			{
				fl.add("goodscode");
				fl.add("goodsname");
				gb += "goodscode,goodsname,";
			}
			
			if (!checkQueryDimension(dimension, "chid"))
				gb += "'' ";
			else
				fl.add("chid");
			gb += "chid,";
			
			sql = "select 0 id," + gb + "count,issuedcount,resultcount,succ,feevalue,requestfee,successfee " +
				"from (select " + fl.toString().replace("[", "").replace("]", "").replace(" ", "") +
				",sum(count) count,sum(issuedcount) issuedcount,sum(resultcount) resultcount,sum(succ) succ,sum(feevalue) feevalue" +
				" ,sum(requestfee) requestfee,sum(successfee) successfee from ipaydata " +
				"where (day>=" + date1.replace("-", "")  + " " +
				"and day<=" + date2.replace("-", "") + ") ";
			if (channel > -1)
				sql += " and channel=" + channel + " ";
//			else
//				sql += " and channel in (select channelid from accountchannel where accountid='" + operator.getId() + "') ";
			if (project > -1)
				sql += " and project=" + project + " ";
			
			if (spId > -2)
				sql += " and spid=" + spId + " ";
//			else
//				sql += " and spid in (select spid from accountsp where accountid='" + operator.getId() + "') ";
			if (spCodeId > -1)
				sql += " and spcodeid=" + spCodeId + " ";
			
			if (province != null && !province.equalsIgnoreCase("ALL"))
				sql += "and province='" + province +"' ";
			if (region != null && !region.equalsIgnoreCase("ALL"))
				sql += "and region='" + region + "' ";
			
			
			if (cpid > -1)
				sql += " and cpid=" + cpid + " ";
			
			if (appnumber != null && !appnumber.equalsIgnoreCase("ALL"))
				sql += "and appid='" + appnumber +"' ";
			
			sql += "group by " + fl.toString().replace("[", "").replace("]", "").replace(" ", "") + ") a " +
					"order by day,hour,minute,channel,project,spid,spcodeid,province,region";
		}
		ModelGenerator<IPayData> dbcpmg = new ModelGenerator<IPayData>(IPayData.class);
		return dbcpmg.generateList(dbcp, sql);
	}

}
